extends BaseEnemy

const VELOCITY = Vector2(0, 1)
const SPEED = 100.0
const SPATIAL_TRANSITION_SPEED = 350

@onready var enemy_sprite: Sprite2D = $EnemySprite
@onready var shot_wait_timer: Timer = $Timer/shot_wait_timer
@onready var ammo_scene = preload("res://scenes/ammo/laser_red.tscn")
@onready var marker_2d: Marker2D = $ShootPoints/Marker2D
@onready var marker_2d_2: Marker2D = $ShootPoints/Marker2D2

var speed: float = SPEED
var pending_damage
#横飞的方向，默认向左
var sideways_velocity = Vector2(1, 0)
var can_shot: bool = false

func _ready() -> void:
	animation_player.play("running")
	shot()
	shot_wait_timer.start()

func _physics_process(_delta: float) -> void:
	if can_shot:
		shot()
		shot_wait_timer.start()
	
	if pending_damage:
		state_machine.change_state("Hurt")

func shot() -> void:
	
	Game.instance_ammo(ammo_scene, marker_2d.global_position, Game.ammo, marker_2d.global_rotation, self)
	Game.instance_ammo(ammo_scene, marker_2d_2.global_position, Game.ammo, marker_2d_2.global_rotation, self)
	can_shot = false

func _on_hurtbox_hurt(hitbox: Hitbox) -> void:
	pending_damage = Damage.new()
	pending_damage.amount = Game.calculate_damage(hitbox.owner, self)
	pending_damage.source = hitbox.owner

func _on_shot_wait_timer_timeout() -> void:
	can_shot = true
